//
//  PeiziWebViewController.swift
//  Community
//
//  Created by MAC on 2019/8/10.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit
import WebKit
class PeiziWebViewController: UIViewController,WKNavigationDelegate,WKUIDelegate {

    var url: String = "http://www.baidu.com"
    var wkWebView: WKWebView!
    var progress:UIProgressView!
    var launchView: LaunchView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
        setupSUbViews()
    }
    

    /// 都说了附件为南方喀算法阿萨德；大法撒旦法地方大的发了弗兰克贾师傅拉菲阿双方均asdl
    /// fadsfaf但是覅安抚as
    func setupSUbViews() {
        self.launchView = LaunchView.loadFromNib()
        self.launchView.frame = UIScreen.main.bounds
    
        wkWebView = WKWebView.init(frame:  UIScreen.main.bounds)
        wkWebView.uiDelegate = self
        wkWebView.navigationDelegate = self
        self.wkWebView.scrollView.bounces = false
        self.wkWebView.scrollView.showsHorizontalScrollIndicator = false;
        self.wkWebView.scrollView.isScrollEnabled = true;
        
        let request = URLRequest.init(url: URL(string: url)!)
        wkWebView.load(request)
        self.view.addSubview(wkWebView)
        self.view.addSubview(launchView)
        
        progress = UIProgressView.init(frame: CGRect(x: 20, y: SCREEN_HEIGHT / 2, width: SCREEN_WIDTH - 40, height: 2))
        self.view.addSubview(progress)
        
        wkWebView.addObserverBlock(forKeyPath: "estimatedProgress") {[weak self] (obj, old, new) in
           
            self?.progress.progress = Float((self?.wkWebView.estimatedProgress)!)
            if (self?.wkWebView.estimatedProgress)! >= 1 {
                self?.progress.removeFromSuperview()
            }
        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            self.launchView.removeFromSuperview()
        }
        
    }
    
    deinit {
        self.wkWebView.removeObserver(self, forKeyPath: "estimatedProgress")
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
